package com.by.car.controller;

import com.by.car.dto.CarWDto;
import com.by.car.service.CarService;
import com.by.car.vo.CarVo;
import com.by.common.core.bean.ResultBean;
import com.by.common.core.response.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Tag(name = "购物车")
@Slf4j
public class CarController {
    @Autowired
    CarService carService;


    @PostMapping ("/car/insert")
    @Operation(summary = "添加商品到购物车")
    public ResultBean carInsert(@RequestBody CarWDto carWDto){
        log.info("购物车新增商品：{}", carWDto);
        int n=carService.carInsert(carWDto);
        return R.success("添加商品到购物车",n);
    }


    @GetMapping("car/select")
    @Operation(summary = "查询购物车商品")
    public ResultBean carSelect(){
        Map<Integer, List<CarVo>> map=carService.carSelect();
        return R.success("查询结果",map);
    }

    @Operation(summary = "删除购物车商品")
    @DeleteMapping("car/delete/{skuId}")
    public ResultBean carDelete(@PathVariable int skuId){
        log.info("删除商品的sku：{}",skuId);
        int n=carService.carDelete(skuId);
        return R.success("删除购物车物品",n);
    }

    @GetMapping("car/skunum")
    @Operation(summary = "查询购物车sku数量上标")
    public ResultBean carSkuNum(){
        int n=carService.carSkuNum();
        return R.success("查询结果",n);
    }

    @PutMapping("car/ischecked")
    @Operation(summary = "更改商品选中状态")
    public ResultBean isChecked(int skuId){
        log.info("更改商品选中状态：{}",skuId);
        int n=carService.isChecked(skuId);
        return R.success("更改商品选中状态",n);
    }

    @PutMapping("car/allischecked/{isChecked}")
    @Operation(summary = "商品状态全选")
    public ResultBean allIsChecked(@PathVariable("isChecked") int isChecked){
        log.info("全选按钮状态改为：{}",isChecked);
        carService.allIsChecked(isChecked);
        return R.success("商品状态全选",isChecked);
    }

    @PutMapping("car/updatenumber")
    @Operation(summary = "修改购物车中商品数量")
    public ResultBean updateNumber(@RequestBody CarWDto carWDto){
        log.info("修改购物车中商品数量：{}", carWDto);
        int n=carService.updateNumber(carWDto);
        return R.success("修改购物车中商品数量",n);
    }

    @PutMapping("car/updatesku")
    @Operation(summary = "修改购物车商品款式")
    public ResultBean updateSku(int skuId,int oldSkuId){
        log.info("修改购物车中商品sku：{}",skuId);
        int n=carService.updateSku(skuId,oldSkuId);
        return R.success("修改购物车中商品sku",n);
    }


    @PostMapping("car/toorder")
    @Operation(summary = "购物车传数据给订单（暂时没用）")
    public ResultBean CatToOrder(@RequestBody List<Integer> skulist){
        log.info("购物车传数据给订单：{}",skulist);
        List<CarWDto> list=carService.CatToOrder(skulist);
        return R.success("购物车传数据给订单",list);
    }


}
